wxapp.tools.file = function() {
    var t = function(t, e) {
        var o = wxapp.objCenter.CreateObject2(e);
        return o.url = wxapp.Const.cdnPath + t, wx.downloadFile(o);
    }, e = function(t, e) {
        return wx.downloadFile({
            url: wxapp.Const.cdnPath + t,
            success: function(t) {
                t && t.statusCode && 404 == t.statusCode ? e && e(!1, {
                    errMsg: "文件下载失败 404错误"
                }) : e && e(!0, t.tempFilePath);
            },
            fail: function(t) {
                e && e(!1, t);
            }
        });
    }, o = function(e, o) {
        t(e, function(t) {
            if (t.isSuccess()) {
                var n = wx.getFileSystemManager();
                try {
                    var r = n.readFileSync(t.getResult().tempFilePath, "utf8");
                    o(r);
                } catch (t) {
                    o("", "读取json文件失败:" + e);
                }
            } else o("", "下载json文件失败:" + e);
        });
    }, n = function(t, e) {
        o(t, function(o, n) {
            if (o) {
                var r = !0;
                try {
                    var i = JSON.parse(o);
                } catch (t) {
                    r = !1;
                }
                r ? e(i) : e(null, "json转换obj失败:" + t);
            } else e(null, n);
        });
    }, r = function(e, o, n, r) {
        var i = new wxapp.ClsLib.WxStopwatch(), s = t(o, function(t) {
            if (i.log2("下载总时间"), !e.isAbort()) {
                var r = {};
                if (r.ok = t.isSuccess(), r.statusCode = t.getResult().statusCode, 0 == r.ok) return r.error_text = "下载zip失败" + o, 
                void n(r);
                wxapp.tools.unzipRes(t.getResult().tempFilePath, function(t, o) {
                    r.ok = t, t || (r.error_text = o), e.isAbort() || n(r);
                });
            }
        });
        s.onProgressUpdate(function(t) {
            r && r(t);
        }), e.downloadTask = s;
    }, i = function(t, o, r, i, s, a, u) {
        var l = wx.getFileSystemManager(), f = function(e) {
            var o = e, n = wx.env.USER_DATA_PATH + "/" + d;
            l.copyFile({
                srcPath: o,
                destPath: n,
                success: function() {
                    a && a((p + 1) / c.filelist.length), t.isAbort() || w();
                },
                fail: function(t) {
                    (t = {}).ok = !1, t.error_text = "散文件 拷贝文件失败:" + n, s && s(t);
                }
            });
        }, c = {}, p = -1, d = "", w = function() {
            if (p++, !t.isAbort()) {
                if (p >= c.filelist.length) {
                    var o = {};
                    return o.ok = !0, u && u(i, c), void (s && s(o));
                }
                d = c.filelist[p], e(r + d, function(e, o) {
                    if (!t.isAbort()) if (e) f(o); else {
                        var n = {};
                        n.ok = !1, n.error_text = "散文件下载失败:" + r + d, s && s(n);
                    }
                });
            }
        };
        n(o, function(e) {
            if (!t.isAbort()) if (e) c = e, wxapp.tools.fileSystem.createFolders(c.filelist), 
            w(); else {
                var n = {};
                n.ok = !1, n.error_text = "散文件下载json失败:" + o, s && s(n);
            }
        });
    };
    return {
        download: function(t, e, o, s) {
            var a = t.data, u = 0, l = 0, f = a.length, c = {};
            c.ok = !0;
            var p = -1;
            !function d() {
                if (!t.isAbort()) if (++p >= a.length) e && e(c); else {
                    var w = a[p];
                    wxapp.twarn("dw", "模块", p, w), 1 === w.type ? r(t, w.path, function(o) {
                        t.isAbort() || (!o || (u += l, l = 0, o.ok) ? n(w.path.substr(0, w.path.length - 3) + "json", function(o, n) {
                            t.isAbort() || (o ? (s && s(w.key, o), d()) : e(!1, n, p, ""));
                        }) : e(!1, o.error_text, p, o));
                    }, function(t) {
                        l = t.progress / 100 * 1 / f, o && o(100 * (u + l));
                    }) : 2 == w.type && i(t, w.path, w.folder, w.key, function(o) {
                        t.isAbort() || (0 != o.ok ? (u += l, l = 0, d()) : e(!1, o.error_text));
                    }, function(t) {
                        l = t / f, o && o(100 * (u + l));
                    }, function(e, o) {
                        t.isAbort() || s && s(e, o);
                    });
                }
            }();
        }
    };
}();